<?php
session_start();
include_once 'objects/User.inc';
if (isset($_SESSION['user'])) {
    $user = unserialize($_SESSION['user']);
} else {
    $user = new User();
}
if (!$user->isLogged()) {
    header("Location: index.php");
    exit();
}

include_once 'objects/CFinal.inc';
include_once 'objects/DCFinal.inc';
include_once 'objects/Servicio.inc';
include_once 'objects/Cliente.inc';

if (isset($_GET['id']))
    $obj = CFinal::EId($_GET['id']);
else
    $obj = new CFinal();
?><!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Factura</title>
        <link rel="stylesheet" type="text/css" href="css/basic.css" />
        <script type="text/javascript" src="script/main.js"></script>
        <style type="text/css">
            body {
                background-color: #CCC;
                color: #000;
                text-align: center;
                font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif;
            }
            table.form {
                text-align: left;
                color: #000;
                background-color: #F5ECB9;
            }
            @media print{
                input[type="button"],table#formd{
                    display: none;
                }
            }
        </style>
    </head>
    <body>
        <input id="tmp" type="hidden">
        <img alt="logo2" src="img/boulevard.jpg" style="margin-top: 3em;"/>
        <?php
        if (isset($_COOKIE['msg'])) {
            echo "<p id=\"msg\" class=\"msg\">" . $_COOKIE['msg'] . "</p>";
            setcookie("msg", "", time() - 3600);
        } else {
            echo "<p id=\"msg\" class=\"error\">" . $obj->msg . "</p>";
        }
        ?>
        <table width="1263" border="0" align='center' cellpadding="3" cellspacing="1" class="form">
            <tr>
                <td colspan="2" width="1428" bgcolor="#CA0000"><b><font color="#FFFFFF" size="3" face="verdana, arial, helvetica">CONSUMIDOR FINAL</font></b></td>
            <tr>
                <td height="238">
                    <table id="form">
                        <tr>
                            <td style="width: 150px;">C&oacute;digo Cliente </td>
                            <td>
                                <input type="hidden" value='<?php echo $obj->encodeId(); ?>' id='formid' />
                                <input type="text" class="cajaPequena" name="skip" id="id_cliente" value='<?php echo $obj->getCliente()->getCode(); ?>'>
                                <img src="img/ver.png" alt="" style="cursor: pointer;" width="16" height="16" title="Buscar cliente" onclick="selectCliente();">
                            </td>
                        </tr>
                        <tr>
                            <td>Cliente <img src="img/cliente.png" alt="" width="16" height="16" title="Validar cliente"></td>
                            <td>
                                <?php
                                $cliente = new Cliente();
                                echo $cliente->getSelect("cliente", $obj->cliente_id);
                                ?>
                            </td>
                        </tr>
                        <tr>
                            <td>Fecha</td>
                            <td >
                                <span><input name="fecha" type="text" size="10" maxlength="10" value="<?php echo date("Y-m-d"); ?>" readonly></span>
                            </td>
                        </tr>
                        <tr>
                            <td>Numero Factura</td>
                            <td>
                                <span><input name="numero_factura" type="text" value="<?php echo $obj->numero_factura; ?>"></span>
                            </td>
                        </tr>
                    </table>
                    <input type="button" name="guardar" id="guardar" value="Guardar" onclick="processForm('cfinal');"/>
                </td>
                <td>
                    <table width="70%" border=0 cellpadding=3 cellspacing=0 id="formd">
                        <tr>
                            <td width="8%">
                                <input type="hidden" value='' id='formdid'/>
                                <input type="hidden" value='<?php echo $obj->encodeId(); ?>' name='factura_id' />Servicio</td>
                            <td >Precio</td>
                            <td >Cantidad</td>
                        </tr>
                        <tr>
                            <td ><?php
                                $servicio = new Servicio();
                                echo $servicio->getSelect("servicio", $obj->servicio_id);
                                ?></td>
                            <td><span>$<input name="precio" type="text" id="precio" size="10" maxlength="10"></span></td>
                            <td>
                                <span><input name="cantidad" type="text" id="cant" size="10" maxlength="10"></span>
                            </td>
                        </tr>
                    </table>
                    <input type="button" value="Agregar" onclick="processFormById('dcfinal', 'formd');"/>
                </td></tr>
            <tr><td>
                    <table style="width: 98%; min-height: 200px;" id='list'>
                        <thead>
                            <tr>
                                <th>Nº</th>
                                <th>Servicio</th>
                                <th>Cantidad</th>
                                <th>Precio Unitario</th>
                                <th>Precio Total</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $total = 0;
                            $ins = new DCFinal();
                            if ($objs = $ins->getAllBy("factura_id", $obj->id)) {
                                $i = 1;
                                foreach ($objs as $obj) {
                                    $onclick = "onclick=\"delItem('" . $obj->encodeId() . "','dcfinal',this)\"";
                                    ?>
                                    <tr>
                                        <td><?php echo $i++; ?></td>
                                        <td><?php echo $obj->getServicio()->servicio; ?></td>
                                        <td><?php echo $obj->cantidad; ?></td>
                                        <td><?php echo sprintf("%01.2f", $obj->precio); ?></td>
                                        <td><?php
                                            $total += ($obj->cantidad * $obj->precio);
                                            echo sprintf("%01.2f", $obj->cantidad * $obj->precio);
                                            ?></td>
                                        <td><img src="img/eliminar.png" alt="delete" title="Delete" <?php echo $onclick; ?> ></td>
                                    </tr>
                                    <?php
                                }
                            } else {
                                echo $ins->msg;
                            }
                            ?>
                        </tbody>
                        <tfoot style="color: #FFFFFF; font-weight: bold;">
                            <tr><td colspan="3">&nbsp;</td><td style="text-align: right;">Total:</td><td><?php echo sprintf("%01.2f", $total); ?></td><td></td></tr>
                        </tfoot>
                    </table>
                </td>
            </tr>
        </table>
        <br><br><br>
        <input type="button" name="button" id="button" value="Borrar" onclick="window.location = 'form_cfinal.php'"/>
        <input type="button" name="cancelar" id="volver" value="Volver" onclick="window.location = 'menu.php';" style="margin-bottom: 3em;"/>
        <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse; border-width: 0; border-color: #111111; height: 53px" width="100%" id="AutoNumber3">
            <tr>
                <td width="100%" height="26" style="border-style: none; border-width: medium" bgcolor="#CA0000">
                    <p align="center"><b><font face="Arial" size="1" color="#000000">Proyectos Informaticos II 2013</font></b>
                </td>
            </tr>
        </table>
    </body>
</html>
